"""
审查记录模块API视图
"""
from rest_framework import viewsets, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from django.db.models import Q
from .models import ReviewRecord
from .serializers import ReviewRecordSerializer, ReviewRecordListSerializer


class ReviewRecordViewSet(viewsets.ReadOnlyModelViewSet):
    """
    审查记录ViewSet（只读）
    
    提供审查记录的查询操作:
        - list: 获取审查记录列表（支持筛选）
        - retrieve: 获取审查记录详情
    """
    queryset = ReviewRecord.objects.all().select_related('student', 'reviewer')
    serializer_class = ReviewRecordSerializer
    permission_classes = [IsAuthenticated]
    
    def get_serializer_class(self):
        """根据不同的action返回不同的序列化器"""
        if self.action == 'list':
            return ReviewRecordListSerializer
        return ReviewRecordSerializer
    
    def get_queryset(self):
        """
        获取查询集，支持按类型、学生、状态筛选
        """
        user = self.request.user
        queryset = ReviewRecord.objects.all().select_related('student', 'reviewer')
        
        # 学生只能看到自己的审查记录
        if not user.is_staff:
            queryset = queryset.filter(student=user)
        
        # 审查类型筛选
        review_type = self.request.query_params.get('review_type', None)
        if review_type:
            queryset = queryset.filter(review_type=review_type)
        
        # 学生筛选
        student_id = self.request.query_params.get('student', None)
        if student_id:
            queryset = queryset.filter(student_id=student_id)
        
        # 状态筛选
        status_filter = self.request.query_params.get('status', None)
        if status_filter:
            queryset = queryset.filter(status=status_filter)
        
        return queryset
